package com.example.demo.demos;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Configuration
public class Allntercept implements WebMvcConfigurer {
    @Autowired
    public  Intercept intercept;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(intercept).addPathPatterns("/**").excludePathPatterns("/method/yes").excludePathPatterns("/index.html");
    }
}
@Configuration
class Intercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession(false);
        if (session==null){
//            response.sendRedirect("/index.html");
            return false;
        }
        return true;
    }
}
